package pri.abner.practice.thread.sequentialexecution;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 *@description 使用blockingQueue 保证顺序执行
 *@author abner chow
 *@date 2019-08-30 17:31
 */
public class Execution4 {

    public static void main(String[] args) {

        BlockingQueue<Thread> blockingQueue = new LinkedBlockingQueue<Thread>();

        Thread t1 = new Thread(new Work(), "Thread one");
        Thread t2 = new Thread(new Work(), "Thread two");
        Thread t3 = new Thread(new Work(), "Thread three");

        blockingQueue.add(t1);
        blockingQueue.add(t2);
        blockingQueue.add(t3);

        for (int i = 0; i < 3; i++) {
            Thread t = null;
            try {
                t = blockingQueue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            t.start();
            //检测线程是否还活着
            while (t.isAlive()) ;
        }

    }

    static class Work implements Runnable{

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName());
        }
    }

}
